/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/game_playing.h"
#include "PlanetWars2dRT-core/specific/controller/game.h"


EXTERN_C_BEGIN


bool
game_playing_run_once(game_playing_t* a_game_playing)
{
  if(!game_state_is_in_pause(&a_game_playing->state))
    {
      if(a_game_playing->do_if_not_paused != NULL &&
	 !a_game_playing->do_if_not_paused(a_game_playing))
	{
	  return false;
	}
      game_update(&a_game_playing->game,
		  game_state_get_update_speed(&a_game_playing->state));
    }
  
  if(a_game_playing->do_always != NULL &&
     !a_game_playing->do_always(a_game_playing))
    {
      return false;
    }
  
  return true;
}

bool
game_playing_run(game_playing_t* a_game_playing)
{
  if(a_game_playing == NULL)
    {
      return false;
    }
  
  while(!game_state_has_to_quit(&a_game_playing->state) &&
	!game_is_finished(&a_game_playing->game))
    {
      if(!game_playing_run_once(a_game_playing))
	{
	  return false;
	}
    }
  return true;
}


EXTERN_C_END
